<template>
  <div class="main">
    <div class="login-form">
      <h2 class="login-title">易果生鲜后台管理系统-登陆</h2>
      <el-form
        status-icon
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
        :label-position="labelPosition"
        label-width="80px"
        :model="ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password" type="password"></el-input>
        </el-form-item>

        <el-form-item prop="keep">
          <!-- <input type="checkbox" @click="keep()" name="" id=""> -->
          <el-checkbox v-model="ruleForm.checked" @click.native="keep($event)">保留7天免登陆</el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button @click="resetForm('ruleForm')" style="float:right; ">重置</el-button>
          <el-button
            type="primary"
            @click="submitForm('ruleForm')"
            style="float:right;margin-right:10px"
          >登陆</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import userApi from "@/api/userApi"; //引入接口
import { setToken, setUser } from "@/utils/auth"; //引入cookie相关方法

export default {
  data() {
    return {
      labelPosition: "right", //右对齐
      ruleForm: {
        username: "",
        password: "",
        checked: false
      },
      rules: {
        //正则
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
          // { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        password: [{ required: true, message: "密码不能为空", trigger: "blur" }]
      }
    };
  },

  components: {},

  methods: {
    //功能：登陆
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          // alert("submit!");
          //验证通过，发送请求
          try {
            // let psw = this.$md5(this.ruleForm.password);
            let p = await userApi.login(
              this.ruleForm.username,
              this.ruleForm.password
              // psw
            );
            if (p.data.flag) {
              //登陆成功 把token和用户信息存到本地(cookie)
              this.$message({
                message: "登陆成功",
                type: "success"
              });

              //存储token和用户信息到本地
              let userinf = {
                username: this.ruleForm.username,
                user_id: p.data.data[0].user_id
              };
              if (this.ruleForm.checked) {
                //保持7天

                setToken(p.data.token, 7);
                setUser(JSON.stringify(userinf), 7);
                //跳转到首页
                this.$router.push("/home");
              } else {
                //会话级别
                setToken(p.data.token);
                setUser(JSON.stringify(userinf));
                this.$router.push("/home");
              }
              //跳转到首页
            } else {
              //登陆失败
              this.$message({
                message: "登陆失败",
                type: "error"
              });
            }
          } catch (err) {}
        } else {
          this.$message({
            message: "服务器问题",
            type: "error"
          });
          return false;
        }
      });
    },

    //功能：解决复选框的坑
    keep(ev) {
      if (ev.target.tagName == "INPUT") return;
      if (!this.ruleForm.checked) {
        this.$message({
          message: "请不要在公共场合保留您的信息",
          type: "error"
        });
      }
    },

    //重制按钮功能
    resetForm(ruleForm) {
      this.$refs[ruleForm].resetFields();
    }
  },

  created() {
    if (this.$route.query) {
      this.ruleForm.name = this.$route.query.name;
    }
  }
};
</script>

<style scoped>
.login-form {
  width: 400px;
  /* 上下间隙150px, 左右自动 */
  margin: 140px auto;
  /*透明背景色*/
  background-color: rgb(255, 255, 255, 0.9);
  padding: 30px;
  padding-bottom: 20px;
  padding-right: 60px;
  /* 圆角 */
  border-radius: 20px;
  /* border: 2px solid orange; */
}
.main {
  position: absolute;
  width: 100%;
  height: 100%;
  background: url("../../assets/img/bg04.jpg") no-repeat;
  background-size: 100% 109%;
  overflow: hidden;
}
/* 标题 */
.login-title {
  text-align: center;
  color: #008842;
  font-family: "STXingkai";
  font-size: 22px;
  /* margin-bottom: 35px; */
  margin: 10px 0 20px 20px;
  /* margin-left: 20px; */
}
</style>